package servlet;

import bean.Goods;
import bean.Page;
import service.impl.BookServiceImpl;
import utils.CreateBeanUtils;
import utils.DataTypeUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet {

    private static final Integer BOOK_PAGE_DEFAULT_SIZE = 5;

    private BookServiceImpl bookService = new BookServiceImpl();

    private static final String pageUrl = "manager/bookServlet?action=page";

    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

//        System.out.println("add()---->"+req.getParameter("pageNumber"));

        Goods goods = CreateBeanUtils.createBeanByParams(Goods.class, req.getParameterMap());

        int i = bookService.addBook(goods);

        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNumber=" +  req.getParameter("pageNumber"));

    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println("delete()---->"+req.getParameter("pageNumber"));

        int id = DataTypeUtils.parseInt(req.getParameter("id"), 0);

        bookService.deleteBookById(id);

        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNumber=" +  req.getParameter("pageNumber"));
    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        Goods goods = CreateBeanUtils.createBeanByParams(Goods.class, req.getParameterMap());

        bookService.updateBook(goods);

        //同步购物车的图书信息
        req.getRequestDispatcher("/cartServlet?action=syncInfo").forward(req,resp);

//        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNumber=" +  req.getParameter("pageNumber"));

    }

    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Goods> goods = bookService.queryAllBook();

        req.setAttribute("books", goods);

        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);
    }

    /**
     * 通过关键字查找
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    protected void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String keyword = req.getParameter("keyword");
        List<Goods> goods = bookService.searchByKeyword(keyword);

    }

        /**
         * 获取需要修改的图书信息，请求转发的修改页面，用于原图书信息显示
         */
    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int id = DataTypeUtils.parseInt(req.getParameter("id"),0);
        Goods goods = bookService.queryById(id);

        req.setAttribute("book", goods);
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);
    }

    /**
     * 分页展示图书
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNumber = DataTypeUtils.parseInt(req.getParameter("pageNumber"),1);
        int pageSize = DataTypeUtils.parseInt(req.getParameter("pageSize"), BOOK_PAGE_DEFAULT_SIZE);
        String classifyName = req.getParameter("classify");
        Page<Goods> page = bookService.pagingQuery(pageNumber, pageSize,classifyName);

        page.setUrl(pageUrl);

        req.setAttribute("page",page);

        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req, resp);

    }

}

